<?php
/**
 * Created by JetBrains PhpStorm.
 * User: DoTuan
 * Date: 10/30/13
 * Time: 10:12 PM
 * To change this template use File | Settings | File Templates.
 */
class Model_admin extends CI_Model{
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }
    //user
    public function getUser(){
        $url ="http://egov047.juddy.vn/api/user/getall";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function getUserByUserName($username){
        $url ="http://egov047.juddy.vn/api/user/GetByUserName?UserName=".$username."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function login($username, $password){
        $url ="http://egov047.juddy.vn/api/Accounts/login?userName=".$username."&pass=".$password."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function addUser(){

    }
    public function getUserById($id){
        $url ="http://egov047.juddy.vn/api/user/GetById?userid=".$id."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function updateUser($userid, $username, $phoneNumber, $email, $avatar, $bio, $groupId){
        $url ="http://egov047.juddy.vn/api/user/Update?userid=".$userid."&userName=".$username."&phoneNumber=".$phoneNumber."&email=".$email."&avatar=".$avatar."&bio=".$bio."&groupId=".$groupId."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function register($username, $password, $phone, $email, $avatar, $bio, $groupId){
        $url ="http://egov047.juddy.vn/api/Accounts/Registration?userName=".$username."&pass=".$password."&phone=".$phone."&email=".$email."&avatar=".$avatar."&bio=".$bio."&groupId=".$groupId."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    //group
    public function getGroup(){
        $url ="http://egov047.juddy.vn/api/group/getall";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function addGroup($name, $description){
        $url ="http://egov047.juddy.vn/api/group/insert?groupName=".$name."&description=".$description."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function updateGroup($gid, $name, $description){
        $url ="http://egov047.juddy.vn/api/group/update?groupId=".$gid."&groupName=".$name."&description=".$description."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function getUserByName($username){
        $url ="http://egov047.juddy.vn/api/user/GetByUserName?UserName=".$username."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function getGroupByName($name){
        $url ="http://egov047.juddy.vn/api/group/GetByName?groupName=".$name."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function addRole($gid, $roleName, $type){
        $url ="http://egov047.juddy.vn/api/role/Insert?groupId=".$gid."&roleName=".$roleName."&type=".$type."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function getGroupById($gid){
        $url ="http://egov047.juddy.vn/api/group/GetById?groupId=".$gid."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function getRoleByGroupId($gid){
        $url ="http://egov047.juddy.vn/api/role/GetByGroupId?groupId=".$gid."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function getRoleByGroupAndType($gid, $type){
        $url ="http://egov047.juddy.vn/api/role/GetByGroupId?groupId=".$gid."&type=".$type."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    public function deleteRole($gid, $roleName){
        $url ="http://egov047.juddy.vn/api/role/Delete?groupid=".$gid."&roleName=".$roleName."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    //Bài viết
    public function getAllPlace()
    {
        $url ="http://egov047.juddy.vn/api/place/getall";
        $json = file_get_contents($url);
        $json_data = json_decode($json, true);
        return $json_data;
    }

    public function getPlaceById($id){
        $url ="http://egov047.juddy.vn/api/place/GetById?Id=".$id."";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }
    
    function array_sort($array, $on, $order=SORT_ASC)
    {
        $new_array = array();
        $sortable_array = array();

        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if ($k2 == $on) {
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }

            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                break;
                case SORT_DESC:
                    arsort($sortable_array);
                break;
            }

            foreach ($sortable_array as $k => $v) {
                $new_array[$k] = $array[$k];
            }
        }

        return $new_array;
    }
    //Location
     public function getLocation(){
        $url ="http://egov047.juddy.vn/api/Location/getall";
        $json = file_get_contents($url);
        $json_data = json_decode($json,true);
        return $json_data;
    }

     public function getAllComment()
    {
        $url ="http://egov047.juddy.vn/api/comment/getall";
        $json = file_get_contents($url);
        $json_data = json_decode($json, true);
        return $json_data;
    }

    public function filter_by_value ($array, $index, $value){ 
        if(is_array($array) && count($array)>0)  
        { 
            foreach(array_keys($array) as $key){ 
                $temp[$key] = $array[$key][$index]; 
                 
                if ($temp[$key] == $value){ 
                    $newarray[$key] = $array[$key]; 
                } 
            } 
        } 
        return $newarray; 
    }

    public function getCommentById($id)
    {
        $url ="http://egov047.juddy.vn/api/comment/getall";
        $json = file_get_contents($url);
        $json_data = json_decode($json, true);
        $nResults = $this->model_admin->filter_by_value($json_data, 'Id', $id);
        return $nResults;
    } 

    public function execURL($url)
    {
        $ch = curl_init();
        $url1 = str_replace(" ", '%20', $url);
        curl_setopt($ch, CURLOPT_URL,$url1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_exec($ch);
        curl_close($ch);
    }
}
?>